package example.bookservlet;


import example.entity.Book;
import example.service.BookService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 获取图书信息
 *
 * @author 张心怡
 * @date 2024/12/16
 */
@WebServlet("/getBookServlet")
public class GetBookServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String id = request.getParameter("id");
        //判断id是否为空 editMode设置为true就是编辑false为新增
        //新增编辑公用addEditBook.jsp页面
        if (id != null && !id.isEmpty()) {
            Book byId = BookService.getBookById(Integer.parseInt(id));
            request.setAttribute("editMode", true);
            request.setAttribute("book", byId);
        } else {
            request.setAttribute("editMode", false);
        }
        request.getRequestDispatcher("addEditBook.jsp").forward(request, response);
    }
}
